package com.shiyu.common.security.handler;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.shiyu.common.core.domain.R;
import com.shiyu.common.core.enums.ResultCode;
import com.shiyu.common.security.exception.ServiceException;
import jakarta.servlet.http.HttpServletRequest;
//import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.rmi.ServerException;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 全局异常处理器
 */
//当有异常抛出,这个类会被调用,针对异常的类型,对异常进行处理
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler
{
//    当发生对应的异常的时候,进入对应的类
//    请求方式不持支的异常
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R<?>
    handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不⽀持'{}'请求", requestURI, e.getMethod());
        return R.fail(ResultCode.ERROR);
    }

//    运行时的异常
    @ExceptionHandler(RuntimeException.class)
    public R<?> handleRuntimeException(RuntimeException e, HttpServletRequest
            request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发⽣异常.", requestURI, e);
        return R.fail(ResultCode.ERROR);
    }
    @ExceptionHandler(ServiceException.class)
    public R<?> handlerServiceException(ServiceException e,HttpServletRequest request){
        String requestURI = request.getRequestURI();
        ResultCode resultCode=e.getResultCode();
        log.error("请求地址'{}',发⽣业务异常.", requestURI, e);
        return R.fail(resultCode);
    }
    @ExceptionHandler(BindException.class)
    public R<Void> handleBindException(BindException e){
        log.error(e.getMessage());
        String message = join(e.getAllErrors(),
                DefaultMessageSourceResolvable::getDefaultMessage, ", ");
        return R.fail(ResultCode.FAILED_PARAMS_VALIDATE.getCode(), message);
    }

    private <E> String join(Collection<E> collection, Function<E, String>
            function, CharSequence delimiter){
        if (CollUtil.isEmpty(collection)) {
            return StrUtil.EMPTY;
        }
        return collection.stream() .map(function) .filter(Objects::nonNull).collect(Collectors.joining(delimiter));
    }
//    兜底的
    @ExceptionHandler(Exception.class)
    public R<?> handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发⽣异常.", requestURI, e);
        return R.fail(ResultCode.ERROR);
    }
//    如果有子类,就不会走父类.
//   子类的优先级比较高
//    slf4j+logbok
}